package org.webbase.common.util;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * 文件操作工具类
 */
public class FileUtil {

	/**
	 * 创建文件夹
	 * @param path
	 */
	public static boolean mkdir(String path) {
		File folder=new File(path);
		if(folder.exists()) return true;
		return folder.mkdirs();
	}
	
	/**
	 * 递归获取文件夹中的文件，从 levelIndex=0 层 到 level 层的所有文件
	 * @param file
	 * @param level
	 * @param levelIndex
	 * @return
	 */
	public static List<File> readDirFile(File file, int level, int levelIndex){
		ArrayList<File> fileList = new ArrayList<File>();
		if(file.isFile()){
			fileList.add(file);
		}else{
			String readPath = file.getAbsolutePath();
			String[] fileChild = file.list();
			if(level == levelIndex){
				return fileList;
			}
			for( int index = 0; index < fileChild.length; index ++ ){
				List<File> list = readDirFile(new File(readPath+"\\"+fileChild[index]),level,levelIndex+1);
				fileList.addAll(list);
			}
		}
		return fileList;
	}
	
	/**
	 * 递归获取文件夹中的文件，从 levelIndex=0 层 到 level 层的所有包含fileName的文件
	 * @param file
	 * @param level
	 * @param levelIndex
	 * @param fileName
	 * @return
	 */
	public static List<File> readDirFile(File file, int level, int levelIndex, String fileName){
		ArrayList<File> fileList = new ArrayList<File>();
		if(file.isFile()){
			if(file.getName().indexOf(fileName) >= 0){
				fileList.add(file);
			}
		}else if(file.isDirectory()){
			String readPath = file.getAbsolutePath();
			String[] fileChild = file.list();
			if(level == levelIndex){
				return fileList;
			}
			for( int index = 0; index < fileChild.length; index ++ ){
				List<File> list = readDirFile(new File(readPath+"\\"+fileChild[index]),level,levelIndex+1,fileName);
				fileList.addAll(list);
			}
		}
		return fileList;
	}
	
	/**
	 * 传入文件夹路径，和需要深入的层数 1 为第一层
	 * @param filePath
	 * @param level
	 * @return
	 */
	public static List<File> getFile(String filePath,int level){
		List<File> fileList = new ArrayList<File>();
		File file = new File(filePath);
		fileList = readDirFile(file,level,0);
		return fileList;
	}
	
	/**
	 * 传入文件夹路径，和需要深入的层数 1 为第一层，和 fileName 文件名中包含的子串
	 * @param filePath
	 * @param level
	 * @param fileName
	 * @return
	 */
	public static List<File> getFile( String filePath, int level, String fileName){
		List<File> fileList = new ArrayList<File>();
		File file = new File(filePath);
		fileList = readDirFile(file,level,0,fileName);
		return fileList;
	}
	
	public static String getContent(String filePath){
		
		return null;
	}
	
	public static String readFileByLinesShowInHtml(String filePath){
		File file = new File(filePath);
		StringBuilder sb = new StringBuilder();
		BufferedReader reader = null;
		try {
			reader = new BufferedReader(new FileReader(file));
			String tmpString;
			int index = 0;
			while( (tmpString = reader.readLine())!= null ){
				sb.append(++index);
				sb.append("、  ");
				sb.append(tmpString);
				sb.append("  <br> ");
			}
			reader.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return sb.toString();
	}
}
